package site.teamo.biu.flink.sdk.job;

import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;

import java.io.Serializable;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author haocongshun
 * @date 2023/07/26 10:55:34
 */
public class JobContext implements Serializable {
    @Getter
    private final String name;

    private final Map<String, JSONObject> configs;

    public JobContext(JobInfo info) {
        this.name = info.getName();
        this.configs = info.getNodes().stream()
                .collect(Collectors.toMap(JobNodeInfo::getName, x -> Optional.ofNullable(x.getConfig()).orElse(new JSONObject())));
    }

    public JSONObject getConfig(String name) {
        return new JSONObject(configs.get(name));
    }
}
